package com.ch.engine.daily.other;

import lombok.Data;

import java.io.*;

/**
 * serialVersionUID 测试类
 *
 * @author wangchunhui
 * @date 2024/9/11 17:59
 */
@Data
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
//    private static final long serialVersionUID = 2L;

    private String name;
    private int age;
    private int id;

    public User(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{name='" + name + "'}";
    }

    public static void main(String[] args) {
        // 序列化对象
//        User user = new User("John");
//        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("user.ser"))) {
//            out.writeObject(user);
//            System.out.println("序列化对象: " + user);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        // 反序列化之前序列化的对象
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("user.ser"))) {
            User user = (User) in.readObject();
            System.out.println("反序列化对象: " + user);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
